<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作业18</title>
    <script>
        /*
        18.  数组转对象；

    ```js

    ['a','a','a','b','b','c','c','c','c','d','d','d','d'] 

    ---> 

    {

        a: 3,

        b: 2,

        c: 4,

        d: 4

    }

    ```
        */

        var arr =  ['a','a','a','b','b','c','c','c','c','d','d','d','d'];

        function hasKey(obj, key) {
            for(var k in obj) {
                if(k == key){
                    return true;
                }
            }
            return false;
        }

        function arrToObj(arr) {
            var obj = {};

            for(var i=0; i<arr.length; i++){
                if(hasKey(obj, arr[i])){
                    obj[arr[i]] += 1;
                }else{
                    obj[arr[i]] = 1;
                }
            }

            return obj;
        }

        var obj = arrToObj(arr);

        for(var k in obj) {
            console.log(k+":"+obj[k]);
        }


    </script>
</head>
<body>
    
</body>
</html>